//
//  BLForgetPsdVC.m
//  Byelone
//
//  Created by 乔小乔 on 2021/2/13.
//

#import "BLForgetPsdVC.h"

@interface BLForgetPsdVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textFieldPhone;
@property (weak, nonatomic) IBOutlet UIButton *btnSendCode;
@property (weak, nonatomic) IBOutlet UITextField *textFieldCode;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPass1;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPass2;
@property (weak, nonatomic) IBOutlet UIButton *btnSubmit;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger totalTime;
@end

@implementation BLForgetPsdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"忘记密码";
    if (self.isReSet) {
        self.navigationItem.title = @"重置密码";
    }
    [self.textFieldPhone addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
}
- (IBAction)clickSendCode:(UIButton *)sender {
    if (!sender.selected) {
        return;
    }
    NSDictionary *dict = @{@"phone":self.textFieldPhone.text?:@"",@"type":@"3"};
    [BLHttpTool postWithPath:BL_USER_SMSCODE params:dict successBlock:^(NSDictionary * _Nonnull obj) {
        sender.selected = NO;
        if (self.timer!=nil) {
            [self.timer invalidate];
            self.timer = nil;
        }
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeHeadle) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        self.totalTime = 60;
        sender.userInteractionEnabled = NO;
        [sender setTitle:@"60s" forState:UIControlStateNormal];
    } failedBlock:^(NSURLSessionDataTask * _Nonnull data, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"%@",error);
        }
    }];
}
- (void)timeHeadle{
    self.totalTime --;
    if (self.totalTime<=0) {
        [self.btnSendCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.btnSendCode.userInteractionEnabled = YES;
        self.btnSendCode.selected = YES;
    }else{
        [self.btnSendCode setTitle:[NSString stringWithFormat:@"%lds",self.totalTime] forState:UIControlStateNormal];
    }
}

- (IBAction)clickScrect:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSInteger index = sender.tag;
    UITextField *text = self.textFieldPass1;
    if (index==102) {
        text = self.textFieldPass2;
    }
    text.secureTextEntry = !sender.selected;
}
- (IBAction)clickSubmit:(UIButton *)sender {
    if (self.textFieldPhone.text.length<11) {
        [HUD showtip:@"请输入正确手机号"];
        return;
    }
    if (self.textFieldCode.text.length<=0) {
        [HUD showtip:@"请输入验证码"];
        return;
    }
    if (self.textFieldPass1.text.length<=0) {
        [HUD showtip:@"请输入密码"];
        return;
    }
    if (self.textFieldPass2.text.length<=0) {
        [HUD showtip:@"请确认密码"];
        return;
    }
    if (![self.textFieldPass1.text isEqualToString:self.textFieldPass2.text]) {
        [HUD showtip:@"密码输入不一致"];
        return;
    }
    NSDictionary *dict = @{@"phone":self.textFieldPhone.text?:@"",@"smsCode":self.textFieldCode.text?:@"",@"password":self.textFieldPass1.text?:@""};
    [BLHttpTool postWithPath:BL_USER_SETPWDPHONE params:dict successBlock:^(NSDictionary * _Nonnull obj) {
        [[BLHUD shareHud] showtip:@"修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failedBlock:^(NSURLSessionDataTask * _Nonnull data, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"%@",error);
        }
    }];
}

#pragma mark - delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.textFieldPhone) {
        if (textField.text.length>=11&&string.length>0) {
            return NO;
        }
    }
    
    return YES;
}
- (void)textChange:(UITextField *)textField{
    if (textField == self.textFieldPhone) {
        if (textField.text.length==11) {
            self.btnSendCode.selected = YES;
        }else{
            self.btnSendCode.selected = NO;
        }
        
    }
}
@end
